﻿using project1113.iniParser;
using project1113.observer;
using project1113.SomeTools;
using project1113.Wrold;
using System;
using System.Text.Json;
using System.Windows;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Tbp.V20190627;
using TencentCloud.Tbp.V20190627.Models;


namespace project1113.SpeechRecoginzer
{
    class TBP
    {
        public TBP()
        {
            Init();
        }
        public string _SecretId { get; set; }
        public string _SecretKey { get; set; }
        public string req_BotId { get; set; }
        public string req_BotEnv { get; set; }
        public string req_TerminalId { get; set; }
        Credential cred;
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile;
        // 实例化要请求产品的client对象,clientProfile是可选的
        TbpClient client;
        // 实例化一个请求对象,每个接口都会对应一个request对象

        public TextProcessRequestJson_for_connection? SendtoBot(string Result)
        {

            try
            {
                TextProcessRequestJson_for_connection resObj = RequestTBPReplay(Result, client);
                return resObj;
            }

            catch (Exception e)
            {
                return null;

            }


        }

        private TextProcessRequestJson_for_connection RequestTBPReplay(string Result, TbpClient client)
        {
            TextProcessRequest req = new TextProcessRequest();
            req.BotId = req_BotId;
            req.BotEnv = req_BotEnv;
            req.TerminalId = req_TerminalId;
            req.InputText = Result;
            // 返回的resp是一个TextProcessResponse的实例，与请求对象对应
            TextProcessResponse resp = client.TextProcessSync(req);
            // 输出json格式的字符串回包        
            string jsonStr = AbstractModel.ToJsonString(resp);
            var resObj = JsonSerializer.Deserialize<TextProcessRequestJson_for_connection>(jsonStr);
            return resObj;
        }
        private void Init()
        {
            string iniFilePath = @"conf\TBPconf.ini";
            var iniParser = new IniParser(iniFilePath,new Observe_err());//打开配置文件,传进去一个观察器
            try
            {
                //尝试读取参数
                _SecretId = iniParser.GetSetting("_SecretId") ?? throw new Exception("缺少参数: _SecretId");
                _SecretKey = iniParser.GetSetting("_SecretKey") ?? throw new Exception("缺少参数: _SecretKey");
                req_BotId = iniParser.GetSetting("req_BotId") ?? throw new Exception("缺少参数: req_BotId");
                req_BotEnv = iniParser.GetSetting("req_BotEnv") ?? throw new Exception("缺少参数: req_BotEnv");
                req_TerminalId = iniParser.GetSetting("req_TerminalId") ?? throw new Exception("缺少参数: req_TerminalId");
            }
            catch(Exception e)
            {
                //读取失败，抛出异常，并且记录到log里面,没有做观察器，偷懒了，看到了记得修改一下
                MessageBox.Show(e.Message);
                StaticLogWrite.Logger.Log(LogLevel.Error, e.Message);
            }
            
            cred = new Credential
            {
                SecretId = _SecretId,
                SecretKey = _SecretKey

            };
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            clientProfile = new ClientProfile();

            // 实例化要请求产品的client对象,clientProfile是可选的
            client = new TbpClient(cred, "", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象



        }
    }
}
